package com.chushouya.manager.dao.repository;

import com.general.framework.core.lang.Lists;
import com.chushouya.manager.dao.entity.RegionEntity;
import com.chushouya.manager.dao.mapper.RegionEntityMapper;
import com.chushouya.manager.dto.admin.region.RegionQuery;
import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Strings;
import com.general.framework.data.mybatis.BaseRepository;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Repository
public class RegionRepository extends BaseRepository<RegionEntityMapper, RegionEntity, Long> {
    public RegionEntity selectOne(RegionQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    public List<RegionEntity> selectList(RegionQuery query) {
        return this.selectByExample(this.buildExample(query));
    }

    private Example buildExample(RegionQuery query) {
        Example example = this.buildExample();
        if (Longs.notNullAndZero(query.getDistrictCode())) {
            example.createCriteria().andEqualTo("districtCode", query.getDistrictCode());
        }
        if (Lists.isNotEmpty(query.getDistrictCodeList())) {
            example.createCriteria().andIn("districtCode", query.getDistrictCodeList());
        }
        if (Strings.isNotBlank(query.getProvince())) {
            example.createCriteria().andEqualTo("province", query.getProvince());
        }
        if (Strings.isNotBlank(query.getCity())) {
            example.createCriteria().andEqualTo("city", query.getCity());
        }
        if (Strings.isNotBlank(query.getDistrict())) {
            example.createCriteria().andEqualTo("district", query.getDistrict());
        }
        if (Longs.notNullAndZero(query.getCityCode())) {
            example.createCriteria().andEqualTo("cityCode", query.getCityCode());
        }
        if (Longs.notNullAndZero(query.getProvinceCode())) {
            example.createCriteria().andEqualTo("provinceCode", query.getProvinceCode());
        }
        return example;
    }

}
